package com.wg.msshopsp.backendmanagement.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wg.msshopsp.backendmanagement.order.entity.OrderVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface OrderVOMapper extends BaseMapper<OrderVO> {
    /**
     * 根据商家用户ID查询其所有店铺的订单
     * @param sellerId 商家用户ID
     * @return 订单列表
     */
    @Select("SELECT " +
            "ordertb.id AS 订单编号, " +
            "ordertb.create_time AS 下单时间, " +
            "ordertb.status AS 订单状态, " +
            "product.name AS 商品名, " +
            "orderdetails.sku AS 规格, " +
            "orderdetails.count AS 数量, " +
            "ordertb.total_amount AS 实付, " +
            "user_buyer.nc AS 用户昵称, " +
            "CONCAT(IFNULL(user_buyer.location, ''), IFNULL(user_buyer.address, '')) AS 用户地址, " +
            "user_buyer.phone AS 用户电话, " +
            "product.id AS 商品编号, " +
            "user_buyer.id AS 用户编号, " +
            "user_buyer.avatar AS 用户头像, " +
            "shop.id AS 店铺编号, " +
            "shop.shop_name AS 店铺名, " +
            "user_seller.id AS 商家用户编号, " +
            "user_seller.nc AS 商家昵称 " +
            "FROM ordertb " +
            "JOIN orderdetails ON ordertb.id = orderdetails.order_id " +
            "JOIN product ON orderdetails.product_id = product.id " +
            "JOIN shop ON product.shop_id = shop.id " +
            "JOIN user AS user_buyer ON ordertb.user_id = user_buyer.id " +
            "JOIN user AS user_seller ON shop.user_id = user_seller.id " +
            "WHERE user_seller.id = #{sellerId} " +
            "ORDER BY shop.id, ordertb.create_time DESC")
    // 显式配置列与属性的映射关系
    @Results({
            @Result(column = "订单编号", property = "orderId"),
            @Result(column = "下单时间", property = "createTime"),
            @Result(column = "订单状态", property = "status"),
            @Result(column = "商品名", property = "productName"),
            @Result(column = "规格", property = "sku"),
            @Result(column = "数量", property = "count"),
            @Result(column = "实付", property = "totalAmount"),
            @Result(column = "用户昵称", property = "buyerNickname"),
            @Result(column = "用户地址", property = "buyerAddress"),
            @Result(column = "用户电话", property = "buyerPhone"),
            @Result(column = "商品编号", property = "productId"),
            @Result(column = "用户编号", property = "buyerId"),
            @Result(column = "用户头像", property = "buyerAvatar"),
            @Result(column = "店铺编号", property = "shopId"),
            @Result(column = "店铺名", property = "shopName"),
            @Result(column = "商家用户编号", property = "sellerId"),
            @Result(column = "商家昵称", property = "sellerNickname")
    })
    List<OrderVO> selectBySellerId(@Param("sellerId") Long sellerId);

    /**
     * 更新单个订单状态（确保商家只能更新自己店铺的订单）
     * @param orderId 订单ID
     * @param newStatus 新状态
     * @param sellerId 商家用户ID
     * @return 影响的行数（1表示成功，0表示失败或无权限）
     */
    @Update("UPDATE ordertb " +
            "JOIN orderdetails ON ordertb.id = orderdetails.order_id " +
            "JOIN product ON orderdetails.product_id = product.id " +
            "JOIN shop ON product.shop_id = shop.id " +
            "JOIN user AS user_seller ON shop.user_id = user_seller.id " +
            "SET ordertb.status = #{newStatus}, " +
            "    ordertb.update_time = NOW() " +  // 同步更新操作时间
            "WHERE ordertb.id = #{orderId} " +
            "  AND user_seller.id = #{sellerId}")
    int updateOrderStatus(@Param("orderId") Long orderId,
                          @Param("newStatus") Integer newStatus,
                          @Param("sellerId") Long sellerId);

    /**
     * 删除单个订单（确保商家只能删除自己店铺的订单）
     * @param orderId 订单ID
     * @param sellerId 商家用户ID
     * @return 影响的行数（1表示成功，0表示失败或无权限）
     */
    @Delete("DELETE ordertb " +
            "FROM ordertb " +
            "JOIN orderdetails ON ordertb.id = orderdetails.order_id " +
            "JOIN product ON orderdetails.product_id = product.id " +
            "JOIN shop ON product.shop_id = shop.id " +
            "JOIN user AS user_seller ON shop.user_id = user_seller.id " +
            "WHERE ordertb.id = #{orderId} " +
            "  AND user_seller.id = #{sellerId}")
    int deleteOrder(@Param("orderId") Long orderId,
                    @Param("sellerId") Long sellerId);
}

